package logic

import (
	"context"
	"errors"
	"video-manager/model/mysql"

	"video-manager/usersrpc/internal/svc"
	"video-manager/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelVideoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelVideoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelVideoLogic {
	return &DelVideoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelVideoLogic) DelVideo(in *usersrpc.DelVideoRequest) (*usersrpc.DelVideoResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User
	if err := findUser.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	var findVideo mysql.Video
	err := findVideo.FindVideoByVid(mysql.DB, in.VideoId)
	if err != nil {
		return nil, err
	}
	if findVideo.ID == 0 {
		return nil, errors.New("视频不存在")
	}
	err = findVideo.DelVideo(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("视频删除失败")
	}
	return &usersrpc.DelVideoResponse{}, nil
}
